﻿@namespace AntDesign.Internal
@inherits AntDomComponentBase
@implements ITableRow
@typeparam TItem
@using AntDesign.TableModels

@if (IsBody)
{
    var rowAttributes = Table.OnRow?.Invoke(RowData);
    if (Table.OnRowClick.HasDelegate)
    {
        rowAttributes ??= new Dictionary<string, object>();
        rowAttributes.TryAdd("onclick", ((Action)delegate { Table.OnRowClick.InvokeAsync(RowData); }));
    }

    <CascadingValue Value="_blockColumns" Name="AntDesign.Column.Blocked">
        <tr @attributes="rowAttributes"
            data-row-key="@(RowData.RowIndex-1)" class="ant-table-row ant-table-row-level-@RowData.Level @(RowData.Selected ? "ant-table-row-selected" : "") @Table.RowClassName(RowData) @rowAttributes?.GetValueOrDefault("class")">
            <CascadingValue Name="AntDesign.Selection.OnChange"
                            Value="EventCallback.Factory.Create<bool>(this, OnSelectionChange)">
                <CascadingValue Value="this" TValue="ITableRow" Name="AntDesign.Selection.TableRow" IsFixed>
                    @ChildContent
                </CascadingValue>
            </CascadingValue>
        </tr>
    </CascadingValue>
}
else if (IsHeader)
{
    var headerRowAttributes = Table.OnHeaderRow?.Invoke();

    <tr @attributes="headerRowAttributes">
        <CascadingValue Name="AntDesign.Selection.OnChange"
                        Value="EventCallback.Factory.Create<bool>(this, OnSelectionChange)">
                @ChildContent
        </CascadingValue>
    </tr>
}

@{ _blockColumns = false; }

@code {
    bool _blockColumns = false;

    [CascadingParameter(Name = "AntDesign.TableRow.Table")]
    public Table<TItem> Table { get; set; }

    [Parameter]
    public RenderFragment ChildContent { get; set; }

    [CascadingParameter(Name = "IsHeader")]
    public bool IsHeader { get; set; }

    [CascadingParameter(Name = "IsBody")]
    public bool IsBody { get; set; }

    [CascadingParameter(Name = "RowData")]
    public RowData<TItem> RowData { get; set; }

    public Selection Selection { get; set; }

    private void OnSelectionChange(bool selected)
    {
        if (IsHeader)
        {
            ((ITable)Table).Selection.RowSelections.Where(x => !x.Disabled).ForEach(x => x.RowData.Selected = selected);
            ((ITable)Table).Selection.Check(selected);
        }
        else if (IsBody)
        {
            RowData.SetSelected(selected);
            if (Selection.Type == "radio")
            {
                ((ITable)Table).Selection.RowSelections.Where(x => x != this.Selection).ForEach(x => x.RowData.Selected = false);
            }
            else
            {
                ((ITable)Table).Selection.StateHasChanged();
            }
        }
    ((ITable)Table).SelectionChanged();
    }

    private async void RowDataSelectedChanged(RowData rowData, bool selected)
    {
        _blockColumns = true;
        await InvokeAsync(StateHasChanged);
    }

    protected override void OnInitialized()
    {
        if (RowData != null)
        {
            RowData.SelectedChanged += RowDataSelectedChanged;
        }
        base.OnInitialized();
    }

    protected override void Dispose(bool disposing)
    {
        if (RowData != null)
        {
            RowData.SelectedChanged -= RowDataSelectedChanged;
        }
        base.Dispose(disposing);
    }
}
